package cn.elead.chaos.remote.web.system.controller;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.excel.ExcelUtil;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.remote.web.system.domain.SysConfig;
import cn.elead.chaos.remote.web.system.service.IRemoteConfigService;
import cn.hutool.core.util.StrUtil;

/**
 * 参数配置Controller
 * 
 * @author luopeng
 * @date 2020-02-26 15:03:09
 */
@RestController
@RequestMapping("/remote/system/config")
public class RemoteConfigController extends BaseController
{
    @Autowired
    private IRemoteConfigService configService;

    /**
     * 查询参数配置列表
     */
    @GetMapping("/page")
    @RequiresPermissions("system:config:list")
    public TableDataInfo<SysConfig> page(SysConfig sysConfig) {
        return configService.page(sysConfig);
    }

    /**
     * 导出参数配置列表
     */
    @GetMapping("/export")
    @RequiresPermissions("system:config:export")
    @Log(title = "参数配置", businessType = BusinessType.EXPORT)
    public String export(SysConfig sysConfig) {
        List<SysConfig> list = configService.list(sysConfig);
        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
        return util.exportExcel(list, "config");
    }

    /**
     * 获取参数配置详细信息
     */
    @GetMapping(value = "/{configId}") 
    @RequiresPermissions("system:config:list")
    public SysConfig getInfo(@PathVariable("configId") Integer configId) {
       	return configService.getInfo(configId);
    }
    
    /**
	 * 根据参数键名查询参数值
	 */
	@GetMapping(value = "/configKey/{configKey}")
	public String getConfigKey(@PathVariable String configKey) {
		return configService.getConfigKey(configKey);
	}

    /**
     * 新增参数配置
     */
    @PostMapping
    @RequiresPermissions("system:config:add")
    @Log(title = "参数配置", businessType = BusinessType.INSERT)
    public int add(@RequestBody SysConfig sysConfig) {
    	return configService.add(sysConfig);
    }

    /**
     * 修改参数配置
     */
    @PutMapping
    @RequiresPermissions("system:config:edit")
    @Log(title = "参数配置", businessType = BusinessType.UPDATE)
    public int edit(@RequestBody SysConfig sysConfig) {
        return configService.edit(sysConfig);
    }

    /**
     * 删除参数配置
     */
	@DeleteMapping("/{configIds}")
    @RequiresPermissions("system:config:remove")
    @Log(title = "参数配置", businessType = BusinessType.DELETE)
    public int remove(@PathVariable Long[] configIds){
		String ids = StringUtils.join(configIds, StrUtil.COMMA);
        return configService.remove(ids);
    }
}